package cn.iocoder.yudao.module.shop.service.memberpay;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.shop.controller.admin.memberpay.vo.ShopMemberPayItemCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.memberpay.vo.ShopMemberPayItemUpdateReqVO;
import cn.iocoder.yudao.module.shop.convert.memberpay.ShopMemberPayConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberpay.ShopMemberPayItemDO;
import cn.iocoder.yudao.module.shop.dal.mysql.memberpay.ShopMemberPayItemMapper;
import cn.iocoder.yudao.module.shop.service.price.ShopPriceService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

@Service
@Validated
public class ShopMemberPayItemServiceImpl implements ShopMemberPayItemService{

    @Resource
    private ShopMemberPayItemMapper shopMemberPayItemMapper;

    @Resource
    private ShopMemberPayService shopMemberPayService;

    @Resource
    private ShopPriceService shopPriceService;

    @Override
    public Long createMemberPayItem(Long priceId, Long memberId) {

        validateMemberPayHad(memberId);

        if(shopPriceService.getPrice(priceId) == null){
            throw exception(PRICE_PACKAGE_NOT_EXISTS);
        }

        // 插入
        ShopMemberPayItemDO createObj = ShopMemberPayConvert.INSTANCE.convertItem(priceId, memberId);
        shopMemberPayItemMapper.insert(createObj);
        // 返回
        return createObj.getId();
    }

    @Override
    public Long updpateMemberPayItem(ShopMemberPayItemUpdateReqVO updateReqVO) {
        validateMemberPayExists(updateReqVO.getId());
        ShopMemberPayItemDO updateObj = ShopMemberPayConvert.INSTANCE.convertItem(updateReqVO);
        shopMemberPayItemMapper.updateById(updateObj);

        return updateObj.getId();
    }

    @Override
    public ShopMemberPayItemDO getMemberPayItemByMemberId(Long memberId){
        ShopMemberPayItemDO shopMemberPayItemDO = shopMemberPayItemMapper.selectOne(new LambdaQueryWrapperX<ShopMemberPayItemDO>()
                .eq(ShopMemberPayItemDO::getMemberId, memberId));
        if(shopMemberPayItemDO == null){
            throw exception(MEMBER_PAY_ITEM_NOT_SET);
        }

        return shopMemberPayItemDO;
    }

    @Override
    public ShopMemberPayItemDO getMemberPayItemByMemberIdNoExp(Long memberId){
        ShopMemberPayItemDO shopMemberPayItemDO = shopMemberPayItemMapper.selectOne(ShopMemberPayItemDO::getMemberId, memberId);
        return shopMemberPayItemDO;
    }

    @Override
    public List<ShopMemberPayItemDO> getMemberPayItemByMemberId(Collection<Long> memberIds){
        List<ShopMemberPayItemDO> list = shopMemberPayItemMapper.selectList(ShopMemberPayItemDO::getMemberId, memberIds);
        return list;
    }

    @Override
    public Long updateMemberPayItem(Long priceId, Long memberId){
        ShopMemberPayItemDO payItemDO = this.getMemberPayItemByMemberId(memberId);
        if(payItemDO == null){
            return -1L;
        }

        payItemDO.setPriceId(priceId);

        shopMemberPayItemMapper.updateById(payItemDO);

        return payItemDO.getId();
    }

    private void validateMemberPayHad(Long memberId) {
        long count = shopMemberPayItemMapper.selectCount(new LambdaQueryWrapperX<ShopMemberPayItemDO>()
                .eq(ShopMemberPayItemDO::getMemberId, memberId));

        if(count > 0) {
            throw exception(MEMBER_PAY_ITEM_HAD_EXISTS);
        }
    }

    private void validateMemberPayExists(Long id) {
        if (shopMemberPayItemMapper.selectById(id) == null) {
            throw exception(MEMBER_PAY_ITEM_NOT_EXISTS);
        }
    }
}
